package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.Authority;
import com.sevattal.service.leaves.mapper.AuthorityMapper;
import com.sevattal.service.leaves.service.AuthorityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class AuthorityServiceImpl extends ServiceImpl<AuthorityMapper, Authority> implements AuthorityService {

    @Override
    public boolean createAuth(Authority auth) {
        QueryWrapper<Authority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_aId",auth.getFAid()).or().eq("f_aName",auth.getFAname());
        int count = baseMapper.selectCount(queryWrapper);
        if (count > 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        int result = baseMapper.insert(auth);
        return result == 1;
    }

    @Override
    public boolean deleteAuth(String id) {
        QueryWrapper<Authority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_aId",id);
        int result = baseMapper.delete(queryWrapper);
        return result == 1;
    }

    @Override
    public boolean updateAuth(Authority auth) {
        QueryWrapper<Authority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_aId",auth.getFAid());
        int count = baseMapper.selectCount(queryWrapper);
        if (count != 1){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        int result = baseMapper.update(auth,queryWrapper);
        return result == 1;
    }

    @Override
    public Authority getByAuthId(String id) {
        QueryWrapper<Authority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_aId",id);
        Authority auth = baseMapper.selectOne(queryWrapper);
        return auth;
    }

    @Override
    public List<Authority> getAuthList() {
        QueryWrapper<Authority> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        List<Authority> authorityList = baseMapper.selectList(queryWrapper);
        return authorityList;
    }
}
